package com.scitc.pleasurevideo;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import com.scitc.pleasurevideo.data.ETMan;
import com.scitc.pleasurevideo.data.SystemSettingDataMan;
import com.scitc.pleasurevideo.data.UserDataMan;
import com.scitc.pleasurevideo.helper.ForwardHelper;
import com.scitc.pleasurevideo.pojo.UserVo;
import com.scitc.pleasurevideo.util.ConstUtil;
import com.scitc.pleasurevideo.util.TimerSettingUtils;
import com.scitc.pleasurevideo.x5webview.MyApplication;

import java.util.Timer;
import java.util.TimerTask;

public class WoFragment extends Fragment {

    private Context context;
    private ImageView settingBtnImg;
    private TextView todayStudyNum;
    private TextView countStudyNum;
    private LinearLayout favoriteBtn;
    private LinearLayout historyBtn;
    private LinearLayout downloadBtn;
    private RelativeLayout timerBtn;
    private TextView timerText;
    private RelativeLayout feedBtn;
    private RelativeLayout appUpdateBtn;
    private RelativeLayout shareBtn;
    private ConstraintLayout clToDayTime;
    private ConstraintLayout clAllTime;
    private ImageView igAvatar;
    private TextView userTitle;

    private Timer timer;
    private Handler handler = new Handler();

    public static WoFragment newInstance() {
        Bundle args = new Bundle();
        WoFragment fragment = new WoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wo, container, false);
        context = getContext();
        initView(view);
        onclick();
        initDownloadUI();
        initTime();
        return view;
    }

    private void initTime() {
    timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        refreshTimerSetting();
                    }
                });
            }
        }, 0, 1000);

    }

    private void initData() {
        UserVo userVo= UserDataMan.init().getUser();
        if(userVo!=null&&userVo.getId()!=null){
            userTitle.setText(userVo.getUser_id());
        }else {
            userTitle.setText("悦视");
        }
        todayStudyNum.setText((ETMan.getPref(getContext(),ConstUtil.APP_PLAY_TIME_SECOND_TODAY,0)+MyApplication.getPlayTimeSecond())/60+"");
        countStudyNum.setText((ETMan.getPref(getContext(),ConstUtil.APP_PLAY_TIME_SECOND_ALL,0)+MyApplication.getPlayTimeSecond())/60+"");
    }

    private void onclick() {
        //设置按钮
        settingBtnImg.setOnClickListener(v -> ForwardHelper.toSettingActivity(getActivity()));
        //搜藏按钮
        favoriteBtn.setOnClickListener(v -> ForwardHelper.toCollectActivity(getActivity()));
        //历史记录
        historyBtn.setOnClickListener(v ->  ForwardHelper.toHistoryActivity(getActivity()));
        //我的下载
        downloadBtn.setOnClickListener(v -> Toast.makeText(context, "暂未开发", Toast.LENGTH_SHORT).show());
        //定时关闭
        timerBtn.setOnClickListener(v -> TimerSettingUtils.showTimerSettingDialog(timerBtn,getContext()));
        //应用更新
        appUpdateBtn.setOnClickListener(v -> Toast.makeText(context, "暂未开发", Toast.LENGTH_SHORT).show());
        //分享给好友
        shareBtn.setOnClickListener(v -> Toast.makeText(context, "暂未开发", Toast.LENGTH_SHORT).show());
        //用户反馈
        feedBtn.setOnClickListener(v -> Toast.makeText(context, "暂未开发", Toast.LENGTH_SHORT).show());
        //今日时间点击事件
        clToDayTime.setOnClickListener(view -> MyApplication.setLoginString(MyApplication.getLoginString()+"0"));
        //总时间点击事件
        clAllTime.setOnClickListener(view -> MyApplication.setLoginString(MyApplication.getLoginString()+"1"));
        //判断是不是展示登录界面
        igAvatar.setOnClickListener(view ->toLogin());
        userTitle.setOnClickListener(view -> toLogin());
    }

    /**
     * 登录事件
     */
    private void toLogin(){
        UserVo userVo= UserDataMan.init().getUser();
        if(userVo!=null&&userVo.getId()!=null){
            Toast.makeText(context, "已经登陆了！！！", Toast.LENGTH_SHORT).show();
        }else {
            if(MyApplication.getLoginString().equals(ConstUtil.LOGIN_STRING)){
                ForwardHelper.toLoginActivity(getActivity());
            }
        }
        MyApplication.setLoginString("");
    }

    private void initView(View view) {
        settingBtnImg = (ImageView) view.findViewById(R.id.settingBtnImg);
        todayStudyNum = (TextView) view.findViewById(R.id.today_study_num);
        countStudyNum = (TextView) view.findViewById(R.id.count_study_num);
        favoriteBtn = (LinearLayout) view.findViewById(R.id.favoriteBtn);
        historyBtn = (LinearLayout) view.findViewById(R.id.historyBtn);
        downloadBtn = (LinearLayout) view.findViewById(R.id.downloadBtn);
        timerBtn = (RelativeLayout) view.findViewById(R.id.timerBtn);
        timerText = (TextView) view.findViewById(R.id.timerText);
        feedBtn = (RelativeLayout) view.findViewById(R.id.feedBtn);
        appUpdateBtn = (RelativeLayout) view.findViewById(R.id.appUpdateBtn);
        shareBtn = (RelativeLayout) view.findViewById(R.id.shareBtn);
        clToDayTime = (ConstraintLayout) view.findViewById(R.id.clToDayTime);
        clAllTime = (ConstraintLayout) view.findViewById(R.id.clAllTime);
        igAvatar = (ImageView) view.findViewById(R.id.igAvatar);
        userTitle = (TextView) view.findViewById(R.id.title);
    }

    /**
     * 判断是否显示下载ui
     */
    private void initDownloadUI() {
        if(MyApplication.isShowDownloadUi){
            downloadBtn.setVisibility(View.VISIBLE);
        }else {
            downloadBtn.setVisibility(View.GONE);
        }
    }

    /**
     * 设置定时关闭的时间显示ui
     */
    private void refreshTimerSetting() {
        String timeStr = SystemSettingDataMan.getDataMan().getSettingCloseTime();
        if (timeStr != null) {
            timerText.setText(timeStr);
        } else {
            timerText.setText("定时关闭");
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if(menuVisible){
            initData();
        }
    }
}
